#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
int n, m, s;
void dijkstra(vector<int> &pathlen, vector << map<int, int> > &graph, int s);
int main()
{
    cin >> n >> m >> s;
    vector<map<int, int>> graph(n);
    vector<int> pathlen(n, INT32_MAX);
    for (int i = 0, u, v, w; i < m; ++i)
    {
        cin >> u >> v >> w;
        if (graph[u][v] != 0 && graph[u][v] > w)
            graph[u][v] = w;
    }
    dijkstra(pathlen, graph, s);
    for (int i : pathlen)
        printf("%d ", i);
}
void dijkstra(vector<int> &pathlen, vector<map<int, int>> &graph, int s)
{
    priority_queue<pair<int, int>, [](pair<int, int> a, pair<int, int> b) -> bool { return a.second < b.second; }> que;
    que.emplace(s, 0);
}